<template>
	<view>
		<u-navbar title="修改群头像" :border-bottom="true"  back-icon-color="#ffffff"
				  :title-size="36"
				  title-color="#ffffff" 
				 :background="{ background: '#000000'}">
			<view class="u-p-r-30" slot="right">
				<u-icon :size="36" color="#ffffff" name="more-dot-fill" @click="chooseImage"></u-icon>
			</view>		
		</u-navbar>
		<view style="width: 100%;padding: 30% 0rpx;">
			<avatar ref='avatar' selWidth="500upx" selHeight="500upx" @upload="myUpload" 
			:avatarSrc="groupAvatar"
				avatarStyle="width:750upx; height: 750upx;">
			</avatar>
		</view>
	</view>
</template>

<script>
	import avatar from "@/components/yq-avatar/yq-avatar.vue";
	export default {
		components: {
			avatar
		},
		data() {
			return {
				background: {
					backgroundColor: '#000000',
				},
				groupId:'',
				groupAvatar:'/static/image/caiyouduo/default-group-avatar.png',
			};
		},
		onLoad:function(e){
			this.groupId=e.groupId;
			if(e.groupAvatar){
				this.groupAvatar=e.groupAvatar;
			}
		},
		methods: {
			chooseImage(){
				this.$refs.avatar.fChooseImg();
			},
			//上传返回图片
			myUpload(rsp) {
				uni.showLoading({
					title:'正在更新'
				});
				let obj = {
					savePath: "userAvatars",
					filePath: rsp.path,
				}
				this.uploadFile(obj);
			},
			
			//上传文件 contentType 2图片 3视频 4语音 5文件
			uploadFile: function(tempFileInfo, contentType) {
				let that = this;
				that.globalUtil.globalUpload(that,{
					param:tempFileInfo,
					success: (uploadResult) => {
						console.log("文件上传完成",uploadResult);
						let fileInfo =uploadResult.result;
						that.groupAvatar=fileInfo.url;
						that.updateGroupAvatar();
						uni.hideLoading();
					},
					fail:(res1) => {
						that.globalUtil.utilAlert("文件上传发送失败");
						uni.hideLoading();
					}
				});
			},
	
			
			updateGroupAvatar:function(){
				let that=this;
				let param={
					"groupId":this.groupId,
					"groupAvatar":this.groupAvatar
				};
				that.localGroupApi.updateGroupInfoFromServer(that,param,(result)=>{
					console.log("修改群聊名称结果",result);
					if(result==true){
						// #ifndef H5
						plus.key.hideSoftKeybord();
						// #endif
						uni.hideKeyboard();
						let timestamp = new Date().getTime();
						let message = {
							"id": new Date().getTime() + "",
							"chatGroupId": that.groupId,
							"userId": that.vuex_userInfo.id,
							"meFlag": true,
							"userName": that.vuex_userInfo.userName,
							"userAvatar": that.vuex_userInfo.avatar ? that.vuex_userInfo.avatar :
								"/static/image/default/default-user/default-user.png",
							'messageType': that.messageApi.MSG_TYPE.SYSTEM_MSG,
							"contentType":  that.messageApi.CONTENT_TYPE.TEXT_CONTENT_TYPE,
							"content": {
								'text':that.vuex_userInfo.userName+"修改群头像",
								'sysMsgType':that.messageApi.SYS_MSG_TYPE.updateGroupAvatar,
								'param':param,
							},
							"createTime": that.globalUtil.getTimeStr(timestamp),
							"timestamp": timestamp,
							"platFrom": 1,
							"showTimeFlag": true,
						}
						console.log("发送修改群名称的系统信息", message);
						this.globalWX.sendMessage(message, {
							complete: function(res) {
								if (res == true) {
									uni.$emit("groupInfoListener",param);
								} else {
									that.globalUtil.utilAlert("网络故障，稍后重试！");
									return;
								}
							}
						},that.messageApi.BUSINESS_CODE.chatting);
						uni.navigateBack({
							delta:1,
							animationType:'pop-out',
							animationDuration:200
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #000000;
	}
</style>
